// pages/shopping/detail.js
var WxParse = require('../../wxParse/wxParse.js');
import {
    store,
    user,
    marketing,
    wxapp
} from '../../api/apiData'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        bannerList: [{
            path: '/images/banner.png',
            url: ''
        }],
        show: false,
        infos: '',
        sku_list: [],
        spec_list: [],
        pidSel: '',
        idSel: '',
        selArr: [],
        selArrStr: '',
        priceSum: 0,
        nums: 1,
        sku_str: '',
        type: 1,
        has_collect: 0,
        commentList:[],
        product_sku_text:'',
        product_sku_id:'',
        showCoupon:false,
        listCou:[],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            id: options.id
        })
        this.getDetail()
        this.getComment()

    },
    toShowCou(){
        this.setData({
            showCoupon:true
        })
    },
    onCloseCou(){
        this.setData({
            showCoupon:false
        })
    },
    toPre(e){
        let imgs = e.currentTarget.dataset.imgs
        let index = e.currentTarget.dataset.index
        wx.previewImage({
          urls: imgs,
          current:index
        })
    },
    getList() {
        marketing.coupon.lists({
            merchant_id:this.data.infos.merchant_id,
            use_scene:3
        }, res => {
            if (res.code == 1) {
                this.setData({
                    listCou: res.data
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    toReceive(e) {
        let id = e.currentTarget.dataset.id
        marketing.coupon.receive({
            coupon_id: id
        }, res => {
            if (res.code == 1) {
                wx.showToast({
                    title: res.msg
                })
               setTimeout(()=>{
                   this.getList()
               },1000)
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    getDetail(){
        store.product.detail({
            product_id: this.data.id
        }, res => {
            if(res.data.detail.spec_type==2){
                let sku_code = []
                res.data.spec_list.forEach(item => {
                    // item.spec_items[0].select = true
                    // sku_code.push(item.spec_items[0].item_id)
                    item.spec_items.forEach(item2=>{
                        res.data.sku_list.forEach(item3=>{
                            if(item3.is_default==1){
                                if(item2.item_id==item3.sku_code){
                                    item2.select = true
                                    sku_code.push(item2.item_id)
                                }
                            }
                        })
                    })
                })
                let sku_str = sku_code.join('_')
                this.setData({
                    sku_str
                })
                res.data.sku_list.forEach(item => {
                    if (sku_str == item.sku_code) {
                        this.setData({
                            priceSum: item.price,
                            product_sku_id: item.id,
                            product_sku_text: item.sku_name_list
                        })
                    }
                })
            }else{
                this.setData({
                    priceSum: res.data.sku_list[0].price,
                    product_sku_id: res.data.sku_list[0].id,
                })
            }
           
            this.setData({
                infos: res.data.detail,
                has_collect: res.data.detail.has_collect,
                sku_list: res.data.sku_list,
                spec_list: res.data.spec_list
            })
            WxParse.wxParse('bindName', 'html', res.data.detail.content, this, 5)
        this.getList()

        })
    },
    getComment(){
        store.comment.lists({product_id:this.data.id,page:1,size:5},res=>{
         if(res.code==1){
             this.setData({
                 commentList:res.data.list
             })
         }
        })
    },
    // 收藏
    toCollect() {
        user.collect.submit({
            module: 'store_product',
            module_id: this.data.infos.id
        }, res => {
            if (res.code == 1) {
                wx.showToast({
                    title: res.data.message,
                })
                let has_collect = this.data.has_collect == 0 ? 1 : 0
                this.setData({
                    has_collect
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    toUrl(e) {
        let url = e.currentTarget.dataset.url
        wx.navigateTo({
            url: url,
        })
    },
    toSelect(e) {
        console.log(e)
        let pidSel = e.currentTarget.dataset.pid
        let idSel = e.currentTarget.dataset.id
        let index = e.currentTarget.dataset.index
        let index2 = e.currentTarget.dataset.index2
        this.setData({
            pidSel,
            idSel
        })
        this.data.selArr.push(idSel)
        this.setData({
            arr2: this.data.selArr
        })
        let list = this.data.spec_list
        list[index].spec_items.forEach(item => {
            item.select = false
        })
        list[index].spec_items[index2].select = true
        this.setData({
            spec_list: list
        })
        let skuList = []
        list.forEach(item => {
            item.spec_items.forEach(item2 => {
                if (item2.select) {
                    skuList.push(item2.item_id)
                }
            })
        })
        let sku_str = skuList.join('_')
        this.setData({
            sku_str
        })
        this.data.sku_list.forEach(item => {
            if (sku_str == item.sku_code) {
                console.log(item.price)
                console.log(this.data.nums)
                this.setData({
                    priceSum: item.price * this.data.nums,
                    product_sku_id: item.id,
                    product_sku_text: item.sku_name_list
                })
            }
        })
    },
    onChange(e) {
        console.log(e)
        let num = e.detail
        this.setData({
            nums: num,
        })
        let sku_str = this.data.sku_str
        console.log(sku_str)
        if(this.data.infos.spec_type==1){
            this.setData({
                priceSum: this.data.sku_list[0].price*num
            })
        }else{
            this.data.sku_list.forEach(item => {
                if (sku_str == item.sku_code) {
                    console.log(item.price)
                    this.setData({
                        priceSum: item.price * num
                    })
                }
            })
        }

       
    },
    toCall() {
        let phone = this.data.infos.merchant.contact_phone
        wx.makePhoneCall({
            phoneNumber: phone,
        })
    },
    toShowTan(e) {
        console.log(e)
        let type = e.currentTarget.dataset.type
        this.setData({
            show: true,
            type: type
        })
    },
    onClose() {
        this.setData({
            show: false
        })
    },
    toConfirm() {
        if(this.data.product_sku_text==''&&this.data.sku_list.length!=0&&this.data.infos.spec_type==2){
            wx.showToast({
              title: '请选择规格',
              icon:'error'
            })
            return false
        }
        if (this.data.type == 1) {
            console.log('加入购物车')
            store.cart.add({
                product_id: this.data.infos.id,
                product_sku_id: this.data.product_sku_id,
                quantity: this.data.nums
            }, res => {
                if (res.code == 1) {
                    wx.showToast({
                        title: res.msg,
                    })
                    this.setData({
                        show: false
                    })
                } else {
                    wx.showToast({
                        title: res.msg,
                        icon: 'error'
                    })
                }

            })
        } else {
            let obj = {
                product_id: this.data.infos.id,
                product_sku_id: this.data.product_sku_id,
                quantity: this.data.nums,
                cover:this.data.infos.cover,
                name:this.data.infos.name,
                price:this.data.priceSum,
                merchantName:this.data.infos.merchant.name,
                product_sku_text: this.data.product_sku_text
            }
            let str = JSON.stringify(obj)
            wx.navigateTo({
                url: '/pages/shopping/order?obj='+str,
            })
        }

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getInfo()
      
    },
    getInfo() {
        let token = wx.getStorageSync('token')
        if (token) {
            // 获取用户详情
            user.detailS({}, res => {
                if(res.code==20003){
					this.getUserInfos()
                }else{
                   
                }
                
            })
        } else {
			this.getUserInfos()

        }
    },
 	/**
	 * 授权登录
	 */
	getUserInfos(e) {
        let that = this
		wx.login({
			success(res) {
				console.log(res)
				let code = res.code
				wx.getUserInfo({
					success: (res) => {
						var encryptedData = res.encryptedData;
						var iv = res.iv
						var info = res.rawData
						var obj = {
							code: code,
							iv: iv,
							encryptedData: encryptedData
						}
						wxapp.login(obj, res => {
							if (res.code == 1) {
								wx.setStorage({
									key: "token",
									data: res.data.token
                                })
                                that.getDetail()
							} else {
								wx.showToast({
									title: res.msg,
									icon: 'none'
								})
							}
						})
					},
					fail: res => {

					}
				})
			}
		})
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})